package com.gf.framework.common.utils.hashhash;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author gaofei
 * @date 2022/6/12 16:41
 */
@Data
@AllArgsConstructor
public class HashHashEntry<K1, K2, V> {
    private final K1 key1;
    private final K2 key2;
    private final V value;

    @Override
    public int hashCode() {
        int hashcode = 0;
        if (key1 != null) {
            hashcode += key1.hashCode();
        }
        if (key2 != null) {
            hashcode += key2.hashCode();
        }
        if (value != null) {
            hashcode += value.hashCode();
        }

        return hashcode;
    }

    @Override
    @SuppressWarnings("unchecked")
    public boolean equals(Object obj){
        if (obj == null || !this.getClass().equals(obj.getClass())) {
            return false;
        }

        HashHashEntry<K1, K2, V> entry = (HashHashEntry<K1, K2, V>)obj;
        if (key1 == null) {
            if (entry.getKey1() != null) {
                return false;
            }
        }
        else if (!key1.equals(entry.getKey1())) {
            return false;
        }

        if (key2 == null) {
            if (entry.getKey2() != null) {
                return false;
            }
            return false;
        }
        else if (!key2.equals(entry.getKey2())) {
            return false;
        }

        if (value == null) {
            return entry.getValue() == null;
        }
        else return value.equals(entry.getValue());
    }
}
